package com.axend.aerosense.user.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModel;

import com.alibaba.android.arouter.launcher.ARouter;
import com.axend.aerosense.base.fragment.MvvmBaseFragment;
import com.axend.aerosense.base.storage.MmkvHelper;
import com.axend.aerosense.common.bean.UserInfo;
import com.axend.aerosense.common.config.Gobal;
import com.axend.aerosense.common.router.RouterActivityPath;
import com.axend.aerosense.common.router.RouterFragmentPath;
import com.axend.aerosense.common.util.FragmentUtil;
import com.axend.aerosense.user.R;
import com.axend.aerosense.user.UserSettingActivity;
import com.axend.aerosense.user.databinding.UserSettingStep2FragmentBinding;

public class UserSettingStep2Fragment extends MvvmBaseFragment<UserSettingStep2FragmentBinding, ViewModel> {
    private UserInfo userInfo;
    public static UserSettingStep2Fragment getInstance(){
        UserSettingStep2Fragment fragment = new UserSettingStep2Fragment();
//        Bundle bundle = new Bundle();
//        bundle.putString(ARG_PARAM, str);
//        fragment.setArguments(bundle);   //设置参数
        return fragment;
    }

    private void setEmailText(){
        userInfo = MmkvHelper.getInstance().getMmkv().decodeParcelable(Gobal.SAVE_KEY.USR_INFO, UserInfo.class);
        ((UserSettingActivity)getActivity()).setTitle(getString(R.string.user_account_safe));
        if(TextUtils.isEmpty(userInfo.userEmail)){
            viewDataBinding.userEmail.setText(getString(R.string.common_un_set));
        }else{
            viewDataBinding.userEmail.setText(userInfo.userEmail);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        setEmailText();
        userInfo = MmkvHelper.getInstance().getMmkv().decodeParcelable(Gobal.SAVE_KEY.USR_INFO, UserInfo.class);
        viewDataBinding.userName.setText(userInfo.userName);
        viewDataBinding.userEmail.setText(userInfo.userEmail);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
//        LiveDatabus.getInstance().with(Gobal.EVENT_KEY.USR_EMAIL_CHANGE, Integer.class)
//                .observe(getViewLifecycleOwner(), temp -> {
//                    initData();
//                });

    }

    private void initView() {
//        viewDataBinding.userEmail.setOnClickListener(v->{
//            changeFragment(UserEmailSettingFragment.getInstance());
//        });

        viewDataBinding.userDevManager.setOnClickListener(v -> {
            changeFragment(UserLoginDevsFragment.getInstance());
        });

        viewDataBinding.userModyPassword.setOnClickListener(v->{
//            UserAdapterFragment.Builder builder = new UserAdapterFragment.Builder()
//                    .setButtonText(getString(R.string.common_complete))
//                    .setPasswordViewVisable(true)
//                    .setTitle(getString(R.string.common_reset_password))
//                    .setCallBack(vv->{
//                        // 进行密码重置
//                        ARouter.getInstance().build(RouterActivityPath.Login.PAGE_LOGIN_HOME).withBoolean("jumpMainPage", false).greenChannel().navigation();
//                        getActivity().finish();
//                    });
//            ((UserSettingActivity)getActivity()).setTitle(getString(R.string.user_password));
//            changeFragment(builder.buildResetPassword());
            Fragment fragment = (Fragment)ARouter.getInstance().build(RouterFragmentPath.Login.PAGE_CHANGE_PASSWORD).navigation();
            changeFragment(fragment);
        });

//        viewDataBinding.userPhone.setOnClickListener(v -> {
//            UserAdapterFragment.Builder builder = new UserAdapterFragment.Builder()
//                    .setButtonText(getString(R.string.common_next))
//                    .setPasswordViewVisable(false)
//                    .setTitle(getString(R.string.common_check_phone))
//                    .setCallBack(vv->{
//                        // 验证当前号码并绑定
//                        doPhoneBinding();
//                    });
//            ((UserSettingActivity)getActivity()).setTitle(getString(R.string.user_phone));
//            changeFragment(builder.buildCheckPhone());
//        });

        viewDataBinding.userCancelNumber.setOnClickListener(v->{
            Fragment fragment = UserCancelNumberFragment.getInstance();
            FragmentUtil.add(getActivity(),((UserSettingActivity)getActivity()).getContentId(),fragment,this,true);
        });
    }


    private void changeFragment(Fragment fragment){
        FragmentUtil.add(getActivity(),
                ((UserSettingActivity)getActivity()).getContentId(),
                fragment,
                this,
                true);
    }


    @Override
    public int getLayoutId() {
        return R.layout.user_setting_step2_fragment;
    }

    @Override
    public int getBindingVariable() {
        return 0;
    }

    @Override
    protected ViewModel getViewModel() {
        return null;
    }

    @Override
    protected void onRetryBtnClick() {

    }
}
